package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.ContractManageDao;
import com.kun.portal.webframe.po.ContractManagePo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */
@Service
public class ContractManageService {
    @Autowired
    private ContractManageDao contractManageDao;
    public List<ContractManagePo> findAllContractManagePo(){
        return contractManageDao.findAll();
    }
     public void saveContractManagePo(ContractManagePo contractManagePo){
        contractManageDao.save(contractManagePo);

    }
    public void deleteContractManagePo(ContractManagePo contractManagePo){
        contractManageDao.delete(contractManagePo);
    }
    public  List<ContractManagePo> checkContractManagePo(String contractNumber ,String contractAgreemen ){

        Specification query = new Specification<ContractManagePo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<ContractManagePo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(contractNumber)){
                    pre.add(cb.like(root.get("contractNumber"), "%"+contractNumber+"%"));
                }
                if(StringUtils.isNotBlank(contractAgreemen )){
                    pre.add(cb.like(root.get("contractAgreemen"), "%"+contractAgreemen+"%"));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return contractManageDao.findAll(query);

    }


}
